package com.andnnl.xiaoshuo;

import cn.hutool.core.date.TimeInterval;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by chenss on 2021/12/22.
 */
public class XiaoShuoFind {
    public static void main(String[] args) {
//        find1();
//        find2();
//        find3();
        List<String> list = findContent("无论别人怎", 10);
//        List<String> list = findContent("在我眼中，一视同仁", 10);
        System.out.println(list);
    }

    private static void find2() {
        try {
            BufferedRandomAccessFile ra = new BufferedRandomAccessFile("D:\\小说\\九星毒奶.txt", "r");
            while ((ra.readLine()) != null) {
//                offsetIndex[i++] = ra.getFilePointer();
            }
            ra.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void find3() {
        try {
            RandomAccessFile ra = new RandomAccessFile("D:\\小说\\九星毒奶.txt", "r");
            while ((ra.readLine()) != null) {
//                offsetIndex[i++] = ra.getFilePointer();
            }
            ra.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void find1() {
        Path path = Paths.get("D:\\小说\\九星毒奶.txt");
        try {
            TimeInterval timeInterval = new TimeInterval();
            Stream<String> countStream = Files.lines(path, Charset.forName("utf-8"));
            AtomicInteger idx = new AtomicInteger();
            countStream.forEach(line -> {
                int i = idx.getAndIncrement();
                if (line.indexOf("在我眼中，一视同仁") != -1) {
                    System.out.println(i + ":" + line);
                }
            });
            System.out.println(timeInterval.intervalSecond());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<String> findContent(String str, int pageSize) {
        List<String> list = new ArrayList<String>();
        try {
            Path path = Paths.get("D:\\小说\\九星毒奶.txt");
            Stream<String> countStream = Files.lines(path, Charset.forName("utf-8"));
            AtomicInteger idx = new AtomicInteger();
            list = countStream.map(line -> {
                int i = idx.getAndIncrement();
                if (line.indexOf(str) != -1) {
//                    System.out.println(i+":"+line);
                    int page = i / pageSize + 1;
                    return (page + ":" + line);
                }
                return null;
            }).filter(s -> s != null).limit(5).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
